package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.tree.DefaultMutableTreeNode;

import controller.Controller;

public class Gui {
	private JFrame frame;
	private Controller controller;
	private JTabbedPane tabs;
	private HostsPane hosts;

	public Gui(Controller controller) {
		frame = new JFrame("NNapper (8-17-10)");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int width = 1280;
		int height = 1024;
		frame.setSize(width, height);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation( (((screen.width)/2)-(width/2)), (screen.height/2)-height/2);
		
		this.controller = controller;
		
		//create tabs
		tabs = new JTabbedPane();
		tabs.setFont(GuiFont.TAB_HEADING.getFont());
		frame.add(tabs);
		
		//create file specification tab
		StartPane fileSpec = new StartPane(this);
		tabs.addTab("Start", fileSpec.getPanel());
		
		frame.setVisible(true);
	}
	public boolean selectFile(){
		return controller.selectFile(frame);
	}
	public String getSelectedFileOrDirectoryFullPath(){
		return controller.getSelectedFileOrDirectoryFullPath();
	}
	public void analyzeFiles(String[] files) {
		controller.analyzeFiles(files, frame);
		
	}
	public void displayAllHosts(DefaultMutableTreeNode tree) {
		
		if (hosts == null){
			hosts = new HostsPane(tree);
			tabs.addTab("All Ports", hosts.getTree());
		}
		else{
			for (int i = 0; i<tabs.getTabCount();i++){
				if (tabs.getTitleAt(i).equals("All Ports")){
					@SuppressWarnings("unused")
					Component pane = tabs.getTabComponentAt(i);
					
					
					hosts.update(tree);
					tabs.updateUI();
					break;
				}
			}
			
		}
		
	}
	public int getTabIndexByTitle(String title){
		//returns the index of a given tab title or -1 if a tab with the title does not exist
		for (int i = 0; i<tabs.getTabCount(); i++){
			if (tabs.getTitleAt(i).equals(title)){
				return i;
			}
		}
		return -1;
	}

}
